/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.dao.hibernate.impl;

import com.mudalov.rec.dao.factory.DAOFactory;
import com.mudalov.rec.dao.factory.MySqlHbnDAOFactory;
import com.mudalov.rec.dao.interfaces.HbnGenericDAO;
import com.mudalov.rec.dao.interfaces.IAdminDAO;
import com.mudalov.rec.dao.model.user.Admin;
import com.mudalov.rec.dao.model.user.Role;
import com.mudalov.rec.util.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author MU
 */
public class HbnAdminDAO extends  HbnGenericDAO<Admin, Long> implements IAdminDAO {

	public HbnAdminDAO(SessionFactory sesssionFactory) {
		this.sessionFactory = sesssionFactory;
	}

	@Override
	protected Class<Admin> getEntityClass() {
		return Admin.class;
	}

    public Admin getAdmin(String login, String password) {
        List<Criterion> criterions = new ArrayList<Criterion>();
        criterions.add(Restrictions.eq("password", password));
        criterions.add(Restrictions.eq("login", login));
        return findDetachedObjectByCriteria(criterions);
    }

    public List<Admin> getAdmins(String fullName, String login, String mail) {
        List<Criterion> criterions = new ArrayList<Criterion>();
        if (!StringUtil.areEmpty(fullName)) {
            criterions.add(Restrictions.like("fullName", "%" + fullName + "%"));
        }
        if (!StringUtil.areEmpty(login)) {
            criterions.add(Restrictions.like("login", "%" + login + "%"));
        }
        if (!StringUtil.areEmpty(mail)) {
            criterions.add(Restrictions.like("mail", "%" + mail + "%"));
        }
        List<Admin> result = findDetachedObjectsByCriteria(criterions);
        return result;
    }

    public static void main(String[] args) {
        DAOFactory factory = DAOFactory.getFactory(DAOFactory.HIBERNATE_MY_SQL);
        List<Admin> result = factory.getAdminDAO().getAdmins("admin", "", "");
    }
}
